﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.标签打印.Service;
using Seagull.BarTender.Print;
using DXApplicationTangche.UC.生产计划.生产批生产.service;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.service;
using System.IO;
using System.Text.RegularExpressions;
using DXApplicationTangche.DTO;
using System.Drawing.Printing;
using DXApplicationTangche.UC.金蝶;
using DXApplicationTangche.Utils;
using mendian;

namespace DXApplicationTangche.UC.标签打印
{
    public partial class Form套号水洗标 : DevExpress.XtraEditors.XtraForm
    {
        private List<washDTO> washDTOs { get; set; } = new List<washDTO>();
        private String standard;
        public Form套号水洗标()
        {
            InitializeComponent();
        }
        public Form套号水洗标(String barcode)
        {
            InitializeComponent();
            this.batchNo.Enabled = false;
            washDTO washDTO = 标签打印Service.getWarterMarkPrintLogByBarcode(barcode);
            try { washDTO.styleSizeCd = washDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { };
            this.washDTOs.Add(washDTO);
            this.gridControl水洗标.DataSource = this.washDTOs;
        }
        private void batchNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrWhiteSpace(this.batchNo.Text))
                {
                    MessageBox.Show("请扫描批号");
                    return;
                }
                try
                {
                    //判断裁剪是否已扫
                    DXApplicationTangche.UC.下单MTO.Service.PlainMessageResult plainMessageResult =
                        生产批生产Service.judgeClippingByBatchNo(this.batchNo.Text.Trim());
                    if (ConstPlainMessageResult.OK.Equals(plainMessageResult.errorCode))
                    {
                        //赋值，给gridControl数据源绑定
                        this.washDTOs = new 标签打印Service(this.batchNo.Text.Trim()).washDTOs;
                    }
                    else
                    {
                        //MessageBoxButtons messBtn = MessageBoxButtons.OKCancel;
                        //DialogResult dr = MessageBox.Show("审版未扫码，是否继续打印洗涤？", "提示:确认", messBtn);
                        //if (dr == DialogResult.OK)
                        //{
                        //    this.washDTOs = new 标签打印Service(this.batchNo.Text.Trim()).washDTOs;
                        //}
                        MessageBox.Show(plainMessageResult.errorMessage);
                        this.washDTOs.Clear();
                    }
                }
                catch
                {
                    MessageBox.Show("无水洗标信息");
                    this.washDTOs.Clear();
                }
                this.gridControl水洗标.DataSource = this.washDTOs;
                this.gridView水洗标.RefreshData();
                this.batchNo.Clear();
            }
        }
        private void repositoryItemButtonEdit1_Click(object sender, EventArgs e)
        {
            try
            {
                //  根据款式查询工信部质量标准
                String styleNameCn = this.washDTOs[this.gridView水洗标.FocusedRowHandle].styleNameCn;
                this.standard = 标签打印Service.getMIITStandardByStyleCode(styleNameCn);
            }
            catch
            {
                this.standard = "FZ/T 73020-2019";
            }

            try
            {
                String printFileName = @"tag\" + "PrintConfig.xml";
                if (!FileUtil.FileExist(printFileName))
                {
                    //将传参的打印机名称保存到配置文件中
                    PrintMachineDTO printMachineDTO传参 = new PrintMachineDTO();
                    new Form打印机配置文件(printMachineDTO传参).ShowDialog();
                    DealXML.ObjectToXMLFile(new PrintMachineDTO("01", printMachineDTO传参.printName), printFileName, Encoding.UTF8);
                }
                PrintMachineDTO printMachineDTO = DealXML.XMLFlieToObject<PrintMachineDTO>(FileUtil.getAbsolutePath(printFileName), Encoding.UTF8);

                //判断成份是否为空，为空不能打印
                if (String.IsNullOrEmpty(this.washDTOs[this.gridView水洗标.FocusedRowHandle].composition))
                {
                    MessageBox.Show("面料成份为空，不能打印水洗");
                    return;
                }
                String result = 标签打印Service.print水洗(this.washDTOs[this.gridView水洗标.FocusedRowHandle], false, printMachineDTO.printName);
                //MessageBox.Show("cnm" + result);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

         }

         private void Form套号水洗标_Load(object sender, EventArgs e)
         {
            this.batchNo.Focus();
            
         }
         

        private void simpleButton上传打印模板_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "btw|*.btw";
            ofd.Title = "请选择btw文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                String str = 标签打印Service.uploadPrintFile(ofd);
                if (!String.IsNullOrWhiteSpace(str))
                {
                    MessageBox.Show("上传成功");
                }
                else
                {
                    MessageBox.Show("上传失败");
                }
            }
        }

        private void simpleButton删除配置文件_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists(@"tag\PrintConfig.xml"))
            {
                File.Delete(@"tag\PrintConfig.xml");
            }
        }
    }
}
